const { BrowserWindow, ipcMain } = require('electron');
const electron = require("electron");
let propertyWindow;

function createPropertyWindow() {
  // if (propertyWindow) {
  //   propertyWindow.focus();
  //   return;
  // }
  const { width, height } = electron.screen.getPrimaryDisplay().bounds;
  propertyWindow = new BrowserWindow({
    width,
    height,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
      webSecurity: false,
    },
    resizable: true,
    transparent: false,
    fullscreen: false,
    frame: true,
  });
  // propertyWindow.webContents.openDevTools();
  const isDev = process.env.NODE_ENV !== 'production';
  // const url = isDev
  //   ? 'http://localhost:8080/#/capital'
  //   : `app://./index.html#/capital`;
  const url = isDev
    ? 'http://localhost:8080/#/visual/property'
    : `app://./index.html#/visual/property`;

  // const url = isDev
  // ? 'http://localhost:8080/#/capital'
  // : `app://./index.html#/capital`;

  propertyWindow.loadURL(url);

  propertyWindow.once('ready-to-show', () => {
    propertyWindow.show();
  });

  propertyWindow.on('closed', () => {
    propertyWindow = null;
  });

  propertyWindow.on("maximize", () => {
  });

  propertyWindow.on("unmaximize", () => {
    const newWidth = 1600;
    const newHeight = 800;

    const x = Math.round((width - newWidth) / 2);
    const y = Math.round((height - newHeight) / 2);

    propertyWindow.setBounds({ width: newWidth, height: newHeight, x, y });
  });
}

function listenPropertyWindowEvents() {
  ipcMain.on('open-property-window', () => {
    createPropertyWindow();
  });
}

module.exports = {
  listenPropertyWindowEvents,
};
